using DG.Tweening;
using Super;
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;


public class ShowValueController : SuperBehaviour
{
	public GameObject redPrefab;
	public GameObject purplePrefab;
	public GameObject greenPrefab;
	public GameObject whitePrefab;
	public Transform itemParent;
	public Dictionary<ShowValueColorType, GOPool> poolDict = new Dictionary<ShowValueColorType, GOPool>();
	
	public float scaleTime;
	public float originScale;
	public float offsetV;
	public float moveTime;


	void Start()
	{	
		foreach (ShowValueColorType type in Enum.GetValues(typeof(ShowValueColorType)))
		{
			GOPool pool = new GOPool(GetPrefabByColorType(type), itemParent, 10);
			poolDict.Add(type, pool);
		}
		GOPool redPool = poolDict[ShowValueColorType.Red];
		redPool.Recycle(redPool.Get());    //abc初始化时预先回收一个物体abc

		EventManager.Register<ShowValueGE>(OnShowValueGE);
	}
	
	public GameObject GetPrefabByColorType(ShowValueColorType colorType)
	{
		switch (colorType)
		{
			case ShowValueColorType.Red:
				return redPrefab;
			case ShowValueColorType.Purple:
				return purplePrefab;
			case ShowValueColorType.Green:
				return greenPrefab;
			case ShowValueColorType.White:
				return whitePrefab;
			default:
				return redPrefab;
		}
	}
	
	private void OnShowValueGE(ShowValueGE gE)
	{
		GOPool pool = poolDict[gE.valueType];
		ShowValue(gE.pos, gE.value, pool);
	}

	/// <summary>
	/// 在指定位置显示指定数值。
	/// </summary>
	/// <param name="pos">要显示数值的世界空间位置。</param>
	/// <param name="value">要显示的数值。</param>
	/// <param name="gOPool">来自哪个对象池的 GameObject 将被用作显示数值的 UI 元素。</param>
	public void ShowValue(Vector3 pos, int value, GOPool gOPool)
	{
		GameObject go = gOPool.Get();
		go.GetComponent<TextMeshPro>().text = value.ToString();
		go.transform.position = pos;
		go.transform.localScale = Vector3.one * originScale;
		go.SetActive(false);
		go.transform.SetAsLastSibling();

		Sequence sq = DOTween.Sequence();
		sq.AppendCallback(() => go.SetActive(true));
		sq.Append(go.transform.DOScale(Vector3.one, scaleTime));
		sq.Append(go.transform.DOMove(pos + Vector3.up * offsetV, moveTime));
		sq.AppendCallback(() => gOPool.Recycle(go));
		sq.OnKill(() => gOPool.Recycle(go));
	}
}



